package com.aiti.lulian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageInfo;
import com.aiti.base.core.page.PageResult;
import com.aiti.lulian.bo.LoginUserInfo;
import com.aiti.lulian.dto.industry.FeaturedIndustryParamDto;
import com.aiti.lulian.dto.industry.FeaturedIndustryQueryDto;
import com.aiti.lulian.entity.industry.CardGovFeaturedIndustry;
import com.aiti.lulian.mapper.card.industry.CardGovFeaturedIndustryMapper;
import com.aiti.lulian.service.CardGovFeaturedIndustryService;
import com.aiti.lulian.utils.BusinessUtil;
import com.aiti.lulian.vo.industry.FeaturedIndustryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: 特色产业service层实现类
 * @auth fcb
 * @date 2025-03-04 14:36
 **/
@Service
@Slf4j
public class CardGovFeaturedIndustryServiceImpl extends BaseSerivceImpl<CardGovFeaturedIndustryMapper, CardGovFeaturedIndustry> implements CardGovFeaturedIndustryService {

    /**
     * 新增特色产业
     * @param featuredIndustryParamDto
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void addFeaturedIndustry(FeaturedIndustryParamDto featuredIndustryParamDto, LoginUserInfo loginUserInfo) {
        // 1. 查询特色产业是否存在
        Integer countByName = this.baseMapper.getCountByName(featuredIndustryParamDto.getIndustryName());
        if(countByName > 0) {
            throw new BusinessException("特色产业【"+featuredIndustryParamDto.getIndustryName()+"】已存在");
        }
        // 2. 新增特色产业
        CardGovFeaturedIndustry industryPo = new CardGovFeaturedIndustry();
        BeanUtil.copyProperties(featuredIndustryParamDto, industryPo);
        industryPo.setBaseId(UUID.randomUUID().toString());
        industryPo.setBaseCreateBy(loginUserInfo.getUserId());
        this.save(industryPo);
    }

    /**
     * 修改特色产业
     * @param paramDto
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void updateFeaturedIndustry(FeaturedIndustryParamDto paramDto, LoginUserInfo loginUserInfo) {
        this.checkParams(paramDto);

        CardGovFeaturedIndustry industryPo = new CardGovFeaturedIndustry();
        BeanUtil.copyProperties(paramDto, industryPo);
        industryPo.setBaseUpdateBy(loginUserInfo.getUserId());

        this.baseMapper.updateFeaturedIndustry(industryPo);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void deleteFeaturedIndustry(List<String> baseIds) {
        if(CollectionUtil.isEmpty(baseIds)) {
            return;
        }
        // 判断这些产业下是否还有产业功能，若有，则不允许删除
        Integer supportCount = this.baseMapper.querySupportCount(baseIds);
        if(supportCount > 0) {
            throw new BusinessException("要删除的特色产业下，还有产业支持，请先删除");
        }
        this.baseMapper.deleteFeaturedIndustry(baseIds);
    }

    /**
     * 查询所有特色产业
     * @return
     */
    @Override
    public List<FeaturedIndustryVo> getFeaturedIndustryList() {
        return this.baseMapper.queryFeaturedIndustryList();
    }

    /**
     * 根据主键查询特色产业
     * @param baseId
     * @return
     */
    @Override
    public FeaturedIndustryVo getFeaturedIndustryById(String baseId) {
        if(StrUtil.isEmpty(baseId)) {
            throw new BusinessException("参数错误");
        }
        CardGovFeaturedIndustry po = this.getById(baseId);
        if(po == null || po.getIsDelete() == 1) {
            throw new BusinessException("数据不存在");
        }
        FeaturedIndustryVo vo = new FeaturedIndustryVo();
        BeanUtil.copyProperties(po, vo);

        return vo;
    }

    /**
     * 分页查询特色产业
     *
     * @return
     */
    @Override
    public PageResult<FeaturedIndustryVo> queryFeaturedIndustryListPage(FeaturedIndustryQueryDto queryDto) {
        BusinessUtil.checkPageParam(queryDto);
        IPage<FeaturedIndustryVo> page = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        List<FeaturedIndustryVo> list = this.baseMapper.queryFeaturedIndustryListPage(page, queryDto);
        PageResult<FeaturedIndustryVo> pageResult = new PageResult<>(list, new PageInfo(queryDto.getPageNum(), queryDto.getPageSize()), page.getTotal());
        return pageResult;
    }

    private void checkParams(FeaturedIndustryParamDto paramDto) {
        if(StrUtil.isEmpty(paramDto.getBaseId())) {
            throw new BusinessException("参数错误");
        }
    }
}
